package com.gjy.statemachine.listener;

import com.gjy.statemachine.module.Order;
import com.gjy.statemachine.state.OrderStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-07-02 11:29:05
 */
@Component
@WithStateMachine(name = "orderStatusMachine")
@Transactional
public class OrderStatusListener {
    private static final Logger log = LoggerFactory.getLogger(OrderStatusListener.class);

    @OnTransition(source = "WAIT_PAYMENT", target = "WAIT_DELIVER")
    public boolean payTransition(Message message) {
        Order order = (Order) message.getHeaders().get("order");
        order.setOrderStatus(OrderStatusEnum.WAIT_DELIVER);
        log.info("支付,状态机反馈信息: {}", message.getHeaders());
        return true;
    }

    @OnTransition(source = "WAIT_DELIVER", target = "WAIT_RECEIVE")
    public boolean deliverTransition(Message message) {
        Order order = (Order) message.getHeaders().get("order");
        order.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE);
        log.info("发货,状态机反馈信息: {}", message.getHeaders());
        return true;
    }

    @OnTransition(source = "WAIT_RECEIVE", target = "FINISH")
    public boolean receiveTransition(Message message) {
        Order order = (Order) message.getHeaders().get("order");
        order.setOrderStatus(OrderStatusEnum.FINISH);
        log.info("收货,状态机反馈信息: {}", message.getHeaders());
        return true;
    }

}
